package com.xdjd.mobile.business.dao.daoservice.activity;

import com.xdjd.mobile.business.dao.mapper.activity.ActivityChallengeRecordDetailPoDao;
import com.xdjd.mobile.business.entity.model.activity.ActivityChallengeRecordDetailPo;
import com.xdjd.mobile.business.util.DateUtil;
import net.oschina.durcframework.easymybatis.PageInfo;
import net.oschina.durcframework.easymybatis.query.Query;
import net.oschina.durcframework.easymybatis.query.Sort;
import net.oschina.durcframework.easymybatis.util.QueryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/1/9 0009.
 */
@Service
public class ActivityChallengeRecordDetailPoService {

    @Autowired
    private ActivityChallengeRecordDetailPoDao activityChallengeRecordDetailPoDao;

    public void updateTodayStepByMemberId(Long memberId, Long stepNumber) {
        activityChallengeRecordDetailPoDao.updateTodayStepByMemberId(memberId, stepNumber);
    }

    public void saveRecordDetail(ActivityChallengeRecordDetailPo po) {
        po.setCreateTime(new Date());
        po.setModifyTime(new Date());
        activityChallengeRecordDetailPoDao.save(po);
    }

    public PageInfo<ActivityChallengeRecordDetailPo> getChallengeRecordList(Long memberId, Integer pageIndex, Integer pageSize) {
        Query query = new Query().eq("member_id", memberId).eq("is_own", 0);
        query.addSort("create_time", Sort.DESC);
        query.page(pageIndex, pageSize);
        return QueryUtils.query(activityChallengeRecordDetailPoDao, query);
    }

    public ActivityChallengeRecordDetailPo findOwnChallengeRecordToday(Long memberId) {
        Query query = new Query().eq("member_id", memberId).eq("is_own", 0).eq("record_date", DateUtil.getDate(new Date()));
        return activityChallengeRecordDetailPoDao.getByExpression(query);
    }

    public Long findOwnChallengeCount(Long memberId) {
        Query query = new Query().eq("member_id", memberId).eq("is_own", 0);
        return activityChallengeRecordDetailPoDao.countTotal(query);
    }

    public List<ActivityChallengeRecordDetailPo> findChallengeMeByRecordNo(String recordNo) {
        Query query = new Query().eq("record_no", recordNo).eq("is_own", 0);
        query.addSort("step_number", Sort.DESC);
        return activityChallengeRecordDetailPoDao.findAll(query);
    }
}
